package com.cbtx.module_sp.ui.activity

import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.text.method.ScrollingMovementMethod
import android.view.View
import com.cbtx.module.base.SpUtils
import com.cbtx.module_sp.MyApplication
import com.cbtx.module_sp.R
import com.cbtx.module_sp.ui.activity.base.MyBaseVMActivity
import com.cbtx.module_sp.ui.dialog.UnbindDialog
import com.cbtx.module_sp.ui.vm.DeviceInfoVM
import com.cbtx.module_sp.utils.NetWorkUtil
import com.txcb.lib.base.utils.DeviceUtil
import kotlinx.android.synthetic.main.activity_device_info.*
import java.text.SimpleDateFormat
import java.util.*

class DeviceInfo : MyBaseVMActivity<DeviceInfoVM>() {

    override fun getPresenterClazz(): Class<DeviceInfoVM> = DeviceInfoVM::class.java

    override fun getLayoutId(): Int = R.layout.activity_device_info

    override fun initView(savedInstanceState: Bundle?) {

        val sf = SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault())
        mips_info.text = sf.format(Date())

        btn_show_qr_code.setOnClickListener {
            val qRCodeDialog = UnbindDialog(this, mVM.ubindQrCode())
            qRCodeDialog.show()
        }

        firmware_des.movementMethod = ScrollingMovementMethod.getInstance()
        firmware_des.text = Build.DISPLAY

        val netWorkState: Int = NetWorkUtil.getNetWorkState(this)
        when (netWorkState) {
            0 -> network_status_des.text = getString(R.string.register_qr_gprs)
            1 -> network_status_des.text = getString(R.string.register_qr_wifi)
            2 -> network_status_des.text = getString(R.string.register_qr_eth)
            else -> network_status_des.text = getString(R.string.register_qr_none)
        }

        if (com.blankj.utilcode.util.NetworkUtils.isConnected()) {
            terminal_status_des.text = "在线"
        } else {
            terminal_status_des.text = "离线"
        }

        mac_des.text = DeviceUtil.getMac(this)

        authorization_info_des.text = "已授权"

        terminal_serial_des.text = MyApplication.mAndroidId

        software_version_des.text = versionName(this)

        ip_add_des.text = NetWorkUtil.getDefaultIp()

        btn_close.setOnClickListener {
            finish()
        }

        position_info_des.movementMethod = ScrollingMovementMethod.getInstance()
        position_info_des.text =SpUtils.getString("storeName") + "/" + SpUtils.getString("roomCode")
    }

    override fun onMyViewClick(view: View) {

    }

    override fun getData() {

    }

    override fun onResume() {
        super.onResume()
        mVM.getStoreInfo()
    }

    fun versionName(context: Context): String {
        val manager = context.packageManager
        var name = ""
        try {
            val info = manager.getPackageInfo(context.packageName, 0)
            name = info.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }
        return name
    }
}